package com.gel.aiagent.tools;

import cn.hutool.Hutool;
import cn.hutool.core.io.FileUtil;
import cn.hutool.http.HttpUtil;
import com.gel.aiagent.contant.FileConstant;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;

import java.io.File;

public class ResourceDownloadTool {
    @Tool(description = "Download a resource from a given URL")
    public String downloadResource(@ToolParam(description = "URL of the resource to download") String url,
    @ToolParam(description = "Name of the file to save the resource as") String fileName) {
        String fileDir= FileConstant.FILE_SAVE_DIR+"/download";
        String filePath=fileDir+"/"+fileName;
        try{
            FileUtil.mkdir(fileDir);
            //使用hutool的downloadFile方法下载文件
            HttpUtil.downloadFile(url,new File(filePath));
            return "Download successful. File saved as: " + filePath;
        }catch (Exception e){
            return "Download failed: " + e.getMessage();
        }


    }
}
